/*
 * <p>Copyright® 2009-2022 东软 IT 实训中心版权所有。</p>
 */

package com.max.back.doctor.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.max.back.doctor.dao.PrescriptionDao;
import com.max.back.doctor.service.PrescriptionService;
import com.max.back.entity.Prescription;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 辛凤文 forward.seen@foxmail.com
 * @version 1.0
 * @description <p>
 * <code>PrescriptionServiceImpl</code>
 *
 * </p>
 * @time 2022/7/27 21:24
 * @see
 */
@Service("/doctorPrescriptionServiceImpl")
public class PrescriptionServiceImpl extends ServiceImpl<PrescriptionDao,
        Prescription> implements PrescriptionService {

    @Autowired
    private PrescriptionDao prescriptionDao;

    public String getPrescriptionIdByByPatientHistory(String patientHistoryId) {
        return prescriptionDao.queryPrescriptionIdByPatientHistory(patientHistoryId);
    }

    @Override
    public String createAndGetPrescription(String patientHistoryId) {
        // 判断是否存在Prescription表
        String id = prescriptionDao.queryPrescriptionIdByPatientHistory(patientHistoryId);
        if (id != null) {
            // 存在直接返回主键id
            return id;
        }
        // 不存在创建Prescription表，参数patientHistoryId
        // 生成主键
        String prescriptionId = IdUtil.simpleUUID();
        // 创建实体对象
        Prescription prescription = new Prescription();
        prescription.setId(prescriptionId);
        prescription.setPatientHistoryId(patientHistoryId);
        System.out.println(patientHistoryId);
        prescription.setDeleted(false);
        // 插入
        if (save(prescription)) {
            return prescriptionId;
        }
        return null;
    }
}